﻿using UnityEngine;

public class SoundData
{
    public bool isMusic = true;
    public bool isSound = true;
    public float musicVolume = 0.5f;
    public float soundVolume = 0.5f;
}

public class SoundMgr : MBSingletonBase<SoundMgr>
{
    private static bool initialized;

    // 通用按钮点击音效
    public AudioClip soundClick;

    private AudioSource musicAudio;
    private AudioSource soundFx;

    public static float MusicVolume
    {
        set => Instance.musicAudio.volume = value;
        get => Instance.musicAudio.volume;
    }

    public static float SoundVolume
    {
        set => Instance.soundFx.volume = value;
        get => Instance.soundFx.volume;
    }

    protected override void Awake()
    {
        base.Awake();
        musicAudio = gameObject.AddComponent<AudioSource>();
        musicAudio.loop = true;
        musicAudio.volume = 0.5f;
        soundFx = gameObject.AddComponent<AudioSource>();
    }

    /// <summary>
    ///     初始化
    /// </summary>
    public void Initialize(SoundData soundData)
    {
        MuteMusic(soundData.isMusic);
        MuteSound(soundData.isSound);
        MusicVolume = soundData.musicVolume;
        SoundVolume = soundData.soundVolume;
        initialized = true;
    }

    private void PlaySound(AudioClip clip, AudioSource audioOut)
    {
        if (clip == null)
        {
            Debug.Log("There are no audio file to play");
            return;
        }

        if (audioOut == musicAudio)
        {
            audioOut.clip = clip;
            audioOut.Play();
        }
        else
        {
            audioOut.PlayOneShot(clip);
        }
    }

    private void PlaySound(AudioClip clip, AudioSource audioOut, float volume)
    {
        if (clip == null)
        {
            Debug.Log("There are no audio file to play");
            return;
        }

        if (audioOut == musicAudio)
        {
            audioOut.clip = clip;
            audioOut.Play();
        }
        else
        {
            audioOut.PlayOneShot(clip);
        }
    }

    public static void MuteMusic(bool isMute)
    {
        Instance.musicAudio.mute = isMute;
    }

    public static void MuteSound(bool isMute)
    {
        Instance.soundFx.mute = isMute;
    }

    /// <summary>
    ///     播放点击音效
    /// </summary>
    public static void Click()
    {
        PlaySfx(Instance.soundClick);
    }

    /// <summary>
    ///     播放指定音效
    /// </summary>
    /// <param name="clip">音频剪辑</param>
    private static void PlaySfx(AudioClip clip)
    {
        Instance.PlaySound(clip, Instance.soundFx);
    }

    /// <summary>
    ///     播放指定音效
    /// </summary>
    /// <param name="clip">音频剪辑</param>
    /// <param name="volume">音量</param>
    public static void PlaySfx(AudioClip clip, float volume)
    {
        SoundVolume = volume;
        Instance.PlaySound(clip, Instance.soundFx, volume);
    }

    /// <summary>
    ///     播放背景音乐
    /// </summary>
    /// <param name="clip">音频剪辑</param>
    public static void PlayMusic(AudioClip clip)
    {
        Instance.PlaySound(clip, Instance.musicAudio);
    }

    /// <summary>
    ///     播放背景音乐
    /// </summary>
    /// <param name="clip">音频剪辑</param>
    /// <param name="volume">音量</param>
    public static void PlayMusic(AudioClip clip, float volume)
    {
        MusicVolume = volume;
        Instance.PlaySound(clip, Instance.musicAudio, volume);
    }
}